package com.doradance.app;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.doradance.R;
import com.doradance.account.Account;
import com.doradance.account.AccountManager;
import com.doradance.account.UrlCenter;
import com.doradance.account.ui.LoginActivity;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.common.http.TokenInfo;
import com.doradance.common.web.CommonWebviewFragment;
import com.doradance.common.web.cache.NativeWebService;
import com.doradance.common.web.cache.UrlMessage;
import com.pachong.android.baseuicomponent.NetUtils;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.SLog;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import cn.jpush.android.api.JPushInterface;

import static com.doradance.account.AccountManager.getTokenInfo;

public class MainActivity extends BaseActivity {
    public static final int REQ_CODE_READ_CONTACT = 0X002;
    public static final int REQ_CODE_READ_CAMERA = 0X003;
    private static boolean isExit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MainBottomFragment menuFrag = new MainBottomFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.lytMain, menuFrag).commit();
        hideToolbar();
        JPushInterface.setAlias(MainActivity.this, getTokenInfo().getUser_id(), null);
        if(isCameraUseable()==true){

        }else{
            //申请WRITE_EXTERNAL_STORAGE权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                    REQ_CODE_READ_CAMERA);

        }
    }



    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == REQ_CODE_READ_CONTACT) {
            if (acceptAllPermissions(permissions, grantResults)) {
            } else {
                // Permission Denied
                Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
            }
        }else if(requestCode == REQ_CODE_READ_CAMERA){
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted

            } else {
                // Permission Denied
                //  displayFrameworkBugMessageAndExit();
                Toast.makeText(this, "请在应用管理中打开“相机”访问权限！", Toast.LENGTH_LONG).show();
            }

        }

    }

    public static void start(Activity activity){
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ) {
            if(!isExit) {
                isExit = true;
                Toast.makeText(this, "在按一次退出程序", Toast.LENGTH_SHORT).show();
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isExit = false;
                    }
                }, 2000);
            } else {
                Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);
                System.exit(0);//退出程序
//                           finish();
            }
        }
        return false;
    }





    boolean acceptAllPermissions(String[] permissions, int[] grantResults) {
        if (grantResults != null && grantResults.length > 0) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
        }

        return true;
    }


    // 打开分享页面


    int shareReqCode = 111;

  /*  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == shareReqCode) {
            if(data != null) {
                SharePlatform platform = (SharePlatform) data.getSerializableExtra(ShareHelper.EXTRA_SHARE_PLATFORM);
                Log.e("vivi", "platform  ==  " + platform.toString()); // 分享平台
                switch (resultCode) {
                    case ShareHelper.SHARE_RESULT_CANCLE:
                        EasyToast.showToast(MainActivity.this, "分享取消");
                        break;

                    case ShareHelper.SHARE_RESULT_FAILED:
                        EasyToast.showToast(MainActivity.this, "分享失败");
                        break;

                    case ShareHelper.SHARE_RESULT_SUCCESS:
                        EasyToast.showToast(MainActivity.this, "分享成功");
                        break;
                }
            }

        }

    }*/

    @Override
    public View createDataView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        return layoutInflater.inflate(R.layout.activity_main, null);
    }


    public  boolean isCameraUseable() {
        boolean canUse =true;
        Camera mCamera =null;
        try{
            mCamera = Camera.open();
// setParameters 是针对魅族MX5。MX5通过Camera.open()拿到的Camera对象不为null
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        }catch(Exception e) {
            canUse =false;
        }
        if(mCamera !=null) {
            mCamera.release();
        }
        return canUse;
    }
    @Override
    protected void onResume() {
        super.onResume();

    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    protected void onStop() {
        super.onStop();
    }
}
